<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height:60px">
              <el-button
                type="primary"
                size="small"
                icon="el-icon-plus"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <el-table border="" :data="list">
              <el-table-column
                header-align="left"
                align="center"
                label="序号"
                type="index"
                width="120px"
              ></el-table-column>
              <el-table-column
                align="center"
                label="名称"
                prop="name"
                width="240px"
              ></el-table-column>
              <el-table-column
                align="center"
                label="描述"
                prop="description"
              ></el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="{ row }">
                  <el-button type="primary" @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button type="success" @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button type="danger" @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination layout="prev,pager,next">

              </el-pagination>
            </el-row> -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 来一个警示框 -->
            <el-alert
              type="info"
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              :show-icon="true"
              :closable="false"
            ></el-alert>

            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input disabled v-model="fomDate.name" style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="fomDate.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="fomDate.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="fomDate.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹出层 -->
    <el-dialog title="分配权限" :visible="showPerDialog">
      <!-- tree 分配权限按钮 -->
      <el-tree
        :data="permData"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
        :props="defaultProps"
        ref="permTree"
      ></el-tree>
      <!-- 发送请求按钮 -->
      <el-row type="flex" slot="footer" justify="center">
        <el-col :span="6">
          <el-button type="primary" @click="btnPermOK">确定</el-button>
          <el-button @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// 引入接口的所有数据
import {
  getTableList,
  getCompanyInfo,
  deleteRole,
  updateRole,
  getRoleDetail,
  addRole,
  assignPerm
} from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
// 引入vuex
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 分配权限弹出层数据
      showPerDialog: false,
      defaultProps: {
        label: 'name'
      },
      permData: [], //专门用来接受tree的数据
      selectCheck: [], //定义一个数组来接收已选中的节点
      roleId: null, //用来记录分配角色的id
      showDialog: false,
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      //
      fomDate: {},
      list: [],
      page: {
        total: 0,
        page: 1,
        pagesize: 2
      }
    }
  },
  // getters 放到计算属性
  computed: {
    ...mapGetters(['companyId'])
  },
  methods: {
    // 分配权限的弹出层
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPerDialog = true
    },
    // 分配权限确定按钮
    async btnPermOK() {
      // console.log(this.$refs.permTree.getCheckedKeys())
      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId
      })
      this.$message.success('恭喜你 分配权限成功')
      this.showPerDialog = false
    },
    // 分配权限取消按钮
    btnPermCancel() {
      this.selectCheck = []
      this.$message('取消成功')
      this.showPerDialog = false
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          // 新增业务
          await addRole(this.roleForm)
        }
        // 三件事
        this.getTableList()
        this.$message('操作成功李路平')
        this.showDialog = false
      } catch (error) {}
    },
    // 编辑打开
    async editRole(id) {
      this.roleFor = await getRoleDetail(id)
      this.showDialog = true
    },
    // 接受全部数据
    async getTableList() {
      const { total, rows } = await getTableList(this.page)
      console.log('llp', total, rows)
      this.list = rows
      this.page.total = total
    },
    // 改变页码
    changePage(val) {
      console.log(val)
      this.page.page = val
      this.getTableList()
    },
    // 创建一个公司的接口
    async getCompanyId(companyId) {
      console.log('ceshi')
      this.fomDate = await getCompanyInfo(this.companyId)
    },
    async deleteRole(id) {
      try {
        await this.$confirm('确定要删除')
        await deleteRole(id)
        this.getTableList()
        this.$message('恭喜你 删除成功')
      } catch (error) {
        console.log(error)
        alert(error)
      }
    }
  },
  mounted() {
    this.getTableList()
    this.getCompanyId()
  }
}
</script>

<style></style>
